package com.zjlanyun.erp.approve;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.aihook.pulltorefresh.lib.XListView;
import com.zjlanyun.erp.R;
import com.zjlanyun.erp.utils.Toastor;

import java.util.ArrayList;
import java.util.HashMap;

public class Approve extends Activity implements XListView.IXListViewListener {

    private TextView title;
    private LinearLayout ll_back;
    private XListView list;
    private SimpleAdapter mSimpleAdapter;
    private ArrayList<HashMap<String, Object>> mList;
    private Handler mHandler;
    private int count = 0;
    private Toastor toastor;
    private Context mContext = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.approve);
        initView();
    }

    //初始化View
    @SuppressLint("NewApi")
    private void initView() {
        title = (TextView) findViewById(R.id.title);
        ll_back = (LinearLayout) findViewById(R.id.ll_back);
        list = (XListView) findViewById(R.id.list);

        title.setText("待处理审批任务");
        toastor = new Toastor();

        //后退
        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });

        //设置列表数据
        mList = new ArrayList<HashMap<String, Object>>();
        list.setPullLoadEnable(true);
        list.setOverScrollMode(View.OVER_SCROLL_NEVER);
        list.setRefreshTime("刚刚");
        mSimpleAdapter = new SimpleAdapter(this, getData(), R.layout.approve_item, new String[]{"billtype", "datetime", "billnumber"}, new int[]{R.id.billtype, R.id.datetime, R.id.billnumber});
        list.setAdapter(mSimpleAdapter);
        mHandler = new Handler();
        list.setXListViewListener(this);
        list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                Intent intent = new Intent(mContext, ApproveFrame.class);
                intent.putExtra("trantype", mList.get(arg2 - 1).get("trantype").toString());
                startActivity(intent);

                //startActivity(Common.getApprovePage(mContext, (String) mList.get(arg2-1).get("trantype")));
            }
        });
    }

    //获取列表数据
    private ArrayList<HashMap<String, Object>> getData() {
        count++;
        if (count > 4) list.setPullLoadEnable(false);
        String[] trantype = {"73", "302", "704", "1", "2", "21"};
        String[] billtype = {"采购申请单", "出货申请单", "收货通知单",
                "采购入库单", "产品入库单", "销售出库单"};
        String[] datetime = {"2015-05-09 10:20", "2015-06-09 10:20", "2015-07-09 10:20",
                "2015-08-10 10:20", "2015-09-12 10:50", "2015-10-20 21:20"};
        String[] billnumber = {"PO201510-001", "PO201510-002", "PO201510-003", "PO201510-004", "PO201510-005", "PO201510-006"};
        for (int i = 0; i < billtype.length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("billtype", billtype[i]);
            map.put("datetime", datetime[i]);
            map.put("billnumber", billnumber[i]);
            map.put("trantype", trantype[i]);
            mList.add(map);
        }
        return mList;
    }

    /**
     * 停止刷新，
     */
    private void onLoad() {
        list.stopRefresh();
        list.stopLoadMore();
        list.setRefreshTime("");
    }

    // 下载刷新
    @Override
    public void onRefresh() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mList.clear();
                getData();
                list.setAdapter(mSimpleAdapter);
                onLoad();
            }
        }, 1200);
    }

    // 加载更多
    @Override
    public void onLoadMore() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                getData();
                mSimpleAdapter.notifyDataSetChanged();
                onLoad();
            }
        }, 500);
    }
}
